<template>
  <div class="login-container">
    <van-nav-bar class="page-nav-bar" title="登录">
      <van-icon
      slot="left"
      name="cross"
      @click="$router.back()"
      />
    </van-nav-bar>
    <van-form @submit="onSubmit" class="font-class" ref="loginForm">
      <van-field
        v-model="user.mobile"
        name="phoneNum"
        placeholder="请输入手机号"
        left-icon="phone-o"
        :rules="formRules.mobile"
        type="number"
        maxlength="11"
      />
      <!-- rules是表单提交验证规则,不满足规则时不提交数据 -->
      <van-field
        v-model="user.code"
        name="验证码"
        placeholder="请输入验证码"
        left-icon="envelop-o"
        :rules="formRules.code"
        type="number"
        maxlength="6"
      >
        <template #button>
          <van-count-down
            :time="1000 * 60"
            v-if="isShowCountDown"
            format="ss s"
            @finish="isShowCountDown = false"
          />
          <van-button
            v-else
            round
            size="small"
            class="sendCommit"
            @click="onSendSms"
            native-type="button"
            >发送验证码</van-button
          >
        </template>
      </van-field>
      <div class="login-btn">
        <van-button class="btn-color" block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>
<script>
import { login, sendSms } from '../../api/user'
export default {
  name: 'loginIndex',
  data () {
    return {
      user: {
        mobile: '',
        code: ''
      },
      formRules: {
        mobile: [{
          required: true,
          message: '手机号不能为空'
        }, {
          pattern: /^1[3|5|7|8]\d{9}$/,
          message: '请输入正确格式的手机号'
        }],
        code: [{
          required: true,
          message: '验证码不能为空'
        }, {
          pattern: /^\d{6}$/,
          message: '请输入正确格式的验证码'
        }]
      },
      isShowCountDown: false
    }
  },
  methods: {
    async onSubmit () {
      const user = this.user
      this.$toast.loading({
        message: '登录中...',
        forbidClick: true, // 背景点击禁用
        duration: 0 // 默认2s，0是一直展示
      })
      try {
        const { data } = await login(user)
        this.$store.commit('setUser', data.data)
        this.$toast.success('登录成功') // 后面的toast会覆盖前面的
        this.$router.push('/my')
      } catch (err) {
        if (err.response.status === 400) {
          this.$toast.fail('登录失败')
        } else {
          this.$toast.fail('未知错误稍后重试')
        }
      }
    },
    async onSendSms () {
      // 发送验证码，需要单独验证手机号（1.验证手机号有效2.倒计时3.ajax请求）
      try {
        await this.$refs.loginForm.validate('phoneNum')
      } catch (err) {
        return this.$toast.fail('手机号验证失败')
      }
      this.isShowCountDown = true
      try {
        await sendSms(this.user.mobile)
        this.$toast('发送成功')
      } catch (err) {
        this.isShowCountDown = false
        this.$toast('发送失败')
      }
    }
  }
}
</script>
<style scoped lang="less">
.sendCommit {
  width: 152px;
  height: 46px;
  font-size: 22px;
  color: #666666;
  background-color: #ededed;
  border: 0;
}
.login-btn {
  margin: 53px 33px;
  border: 0;
  .btn-color {
    background-color: #6db4fb;
    font-size: 30px;
  }
}
</style>
